package com.xq.jltx.frescosimple.adapters;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.xq.jltx.frescosimple.R;
import com.xq.jltx.frescosimple.display.DefaultDrawables;
import com.xq.jltx.frescosimple.display.FrescoDraweeView;
import com.xq.jltx.frescosimple.display.ImageLoadeInstrumented;
import com.xq.jltx.frescosimple.display.PerfListener;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016年02月02日.
 */

public class FrescoListViewAdapter extends ImageListAdapter<FrescoListViewAdapter.ViewHolder>{
    private static String TAG = "FrescoListViewAdapter";
    private LayoutInflater inflater;
    private Context context;
    private PerfListener mPerfListener;
    private String[]  imageList;

    public FrescoListViewAdapter(Context context, String[] imageList, ImagePipelineConfig imagePipelineConfig, PerfListener mPerfListener) {
        super(imageList, mPerfListener);
        Fresco.initialize(context, imagePipelineConfig);
        this.context = context;
        this.imageList = imageList;
        this.mPerfListener = mPerfListener;
        init();
    }

    public void init(){
//        inflater = (LayoutInflater) context.getSystemService(AppCompatActivity.LAYOUT_INFLATER_SERVICE);
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected Class<ViewHolder> getViewClass() {
        return ViewHolder.class;
    }

    @Override
    protected ViewHolder createViewHolder() {
        ViewHolder viewHolder = new ViewHolder();
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setBorder(context.getResources().getColor(android.R.color.holo_red_light),3.0f);
        roundingParams.setRoundAsCircle(true);
        roundingParams.setCornersRadius(72f);
        /**
         *这里是一个坑，不同的DraweeView不能用同一个GenericDraweeHierarchy
         */
        GenericDraweeHierarchy displayCoverImgDH = new GenericDraweeHierarchyBuilder(context.getResources())
                .setPlaceholderImage(DefaultDrawables.sPlaceholderDrawable, ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(DefaultDrawables.sErrorDrawable,ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(roundingParams)
                .setActualImageScaleType(GenericDraweeHierarchyBuilder.DEFAULT_ACTUAL_IMAGE_SCALE_TYPE)
//                .setProgressBarImage(new ProgressBarDrawable())
                .build();

        View view = inflater.inflate(R.layout.item_list_image,null,false);
        viewHolder.frescoDraweeView = (FrescoDraweeView)view.findViewById(R.id.image);
        viewHolder.textView = (TextView)view.findViewById(R.id.text);
        viewHolder.frescoDraweeView.setHierarchy(displayCoverImgDH);
        viewHolder.rootView = view;
        return viewHolder;
    }

    @Override
    protected void bind(ViewHolder viewHolder, String uri) {
        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
                        .setResizeOptions(new ResizeOptions(200, 200))
                        .setAutoRotateEnabled(true)   //自动旋转
                        .setProgressiveRenderingEnabled(true) //我们必须显式地在加载时，允许渐进式JPEG图片加载
                        .build();
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequest)
                .setOldController(viewHolder.frescoDraweeView.getController())
                .setControllerListener(controllerListener)
                .setAutoPlayAnimations(true)
                .build();
        viewHolder.frescoDraweeView.setController(draweeController);
        viewHolder.textView.setText(uri);
    }

    ControllerListener controllerListener = new BaseControllerListener() {
        @Override
        public void onSubmit(String id, Object callerContext) {
            super.onSubmit(id, callerContext);
        }

        @Override
        public void onFinalImageSet(String id, Object imageInfo, Animatable animatable) {
            super.onFinalImageSet(id, imageInfo, animatable);
        }

        @Override
        public void onIntermediateImageSet(String id, Object imageInfo) {
            super.onIntermediateImageSet(id, imageInfo);
        }

        @Override
        public void onIntermediateImageFailed(String id, Throwable throwable) {
            super.onIntermediateImageFailed(id, throwable);
        }

        @Override
        public void onFailure(String id, Throwable throwable) {
            super.onFailure(id, throwable);
        }

        @Override
        public void onRelease(String id) {
            super.onRelease(id);
        }
    };
    @Override
    public void shutDown() {
        Fresco.shutDown();
    }

    public static class ViewHolder implements ImageLoadeInstrumented {
        public View rootView;
        public FrescoDraweeView frescoDraweeView;
        public TextView textView;

        @Override
        public void initInstrumentation(String tag, PerfListener perfListener) {

        }

        @Override
        public View getItemRootView() {
            return rootView;
        }
    }
}
